package com.game;

import java.util.HashMap;
import java.util.Map;

import javazoom.jl.player.Player;

import processing.core.PApplet;

import com.game.screen.GameOverScreen;
import com.game.screen.GameScreen;
import com.game.screen.IntroScreen;
import com.game.screen.MainScreen;
import com.game.screen.Screen;
import com.game.util.MP3;
import com.game.util.MP3_2;
import com.game.util.ProcessingObject;

public class GameJam extends PApplet {
	
	public String nextScreen;

	public float alpha = 0f; 
	
	public final static String GAME_SCREEN = "game";
	public final static String MAIN_SCREEN = "main";
	public final static String INTRO_SCREEN = "intro";
	public final static String GAME_OVER_SCREEN_OK = "overOkay";
	public final static String GAME_OVER_SCREEN_PERFECT = "overPerfect";
	public final static String GAME_OVER_SCREEN_GREAT = "overGreat";
	private Map<String, Screen> screens = new HashMap<String, Screen>();
	public Screen currentScreen;
	
	private MP3_2 sound;
	private Player player;
	
	private int time;

	private final static int FADE_IN = 1;
	private final static int FADE_OUT = 2;
	private final static int NO_FADE = 3;
	
	private int mode = NO_FADE;
	
	public void setup(){
		size(300, 400);
		
		time = millis();
		
		smooth();
		
		ProcessingObject.pApplet = this;

		GameScreen gameScreen = new GameScreen();
		gameScreen.img = "boyL2.png";
		screens.put(GAME_SCREEN, gameScreen);

		MainScreen mainScreen = new MainScreen();
		mainScreen.img = "mainscreen.png";
		screens.put(MAIN_SCREEN, mainScreen);
		
		IntroScreen introScreen = new IntroScreen();
		introScreen.img = "instructions.png";
		screens.put(INTRO_SCREEN, introScreen);
		
		GameOverScreen gameOverScreenOk = new GameOverScreen();
		gameOverScreenOk.img = "ending1.png";
		screens.put(GAME_OVER_SCREEN_OK, gameOverScreenOk);
		
		GameOverScreen gameOverScreenGreat = new GameOverScreen();
		gameOverScreenGreat.img = "ending2.png";
		screens.put(GAME_OVER_SCREEN_GREAT, gameOverScreenGreat);
		
		GameOverScreen gameOverScreenPerfect = new GameOverScreen();
		gameOverScreenPerfect.img = "ending3.png";
		screens.put(GAME_OVER_SCREEN_PERFECT, gameOverScreenPerfect);
		
		for(Screen screen: screens.values()){
			screen.init();
		}
		
		currentScreen = mainScreen;
		
		sound = new MP3_2("http://www.madparker.com/lucid.mp3");

		player = sound.play();
	}
	
	public void draw(){
		if(millis() - time > 1000 * 2 * 60 + 5000){
			
			time = millis();
			System.out.println("SONG RESTART");
			sound = new MP3_2("http://www.madparker.com/lucid.mp3");
			player = sound.play();
		}
		
		if(mode == FADE_OUT){
			alpha += 2.0f;
			tint(0, 0, 0, alpha); 
		}
		
		if((alpha > 100) && (mode == FADE_OUT)){
			mode = FADE_IN;
			currentScreen = screens.get(nextScreen);
		}

		
		if((alpha < 0) && (mode == FADE_IN)){
			mode = NO_FADE;
			alpha = 0f;
			tint(255, 255, 255, 255); 
		}
		
//		System.out.println(alpha);
		
		if(mode == FADE_IN){
			alpha -= 2.0f;
			tint(255, 255, 255, alpha); 
		}
		
		currentScreen.update();
		currentScreen.display();
	}
	@Override
	public void mousePressed() {
		currentScreen.mousePressed();
	}

	public void keyPressed() {
		
		if(keyPressed)
			currentScreen.onKeyPressed(key, keyCode);
		
	}
	
	public void updateScreen(String screenName){
		nextScreen = screenName;
		
		mode = FADE_OUT;
//		currentScreen = screens.get(screenName);
	}
	
	public void reset(){
		GameScreen gameScreen = (GameScreen)screens.get(GAME_SCREEN);
		
		gameScreen.resetPlayer();
	}
	

	/**** Standard ****/
	
	static public void main(String _args[]) {
		PApplet.main(new String[] { myClassName });
	}
	
	protected static String myClassName = getQualifiedClassName();
	
	public static String getQualifiedClassName() {
		return new Exception().getStackTrace()[1].getClassName();
	}
}